package com.kaili.basic.service.impl;

/**
 * Created by xxx on 2019-03-07.
 */

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.*;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.*;
import com.kaili.basic.service.WarehousingApplicationService;
import com.kaili.common.util.LoginDetailUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xxx
 * @Copyright: Copyright (c) 2017
 * @Company: 杭州凯立通信有限公司
 */
@Service
public class WarehousingApplicationServiceImpl implements WarehousingApplicationService {
    @Autowired
    WarehousingApplicationMapper warehousingApplicationMapper;
    @Autowired
    WarehousingProductMapper warehousingProductMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    WarehousingReturnProductMapper warehousingReturnProductMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int addWarehousingApplication(WarehousingApplicationAddDto warehousingApplicationAddDto) {
        WarehousingApplication warehousingApplication = new WarehousingApplication();
        BeanUtils.copyProperties(warehousingApplicationAddDto, warehousingApplication);
        Long applyId = Long.parseLong(warehousingApplication.getRequestCode());
        /*Long applyId = warehousingApplicationMapper.getMaxApplyId();
        applyId = applyId + 1L;
        warehousingApplication.setRequestCode(StringUtils.leftPad(applyId.toString(), 6, "0"));*/
        warehousingApplication.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        warehousingApplication.setApplyId(applyId);
        Supplier supplier=supplierMapper.querySupplierById(warehousingApplication.getSupplierId());
        if(supplier!=null){
            warehousingApplication.setSupplierCode(supplier.getSupplierCode());
            warehousingApplication.setSupplierName(supplier.getSupplierName());
        }
        warehousingApplicationMapper.addWarehousingApplication(warehousingApplication);
        int order=1;
        for (WarehousingProductDto warehousingProductDto : warehousingApplicationAddDto.getWarehousingProductList()) {
            WarehousingProduct warehousingProduct=new WarehousingProduct();
            BeanUtils.copyProperties(warehousingProductDto, warehousingProduct);
            warehousingProduct.setApplyId(applyId);
            warehousingProduct.setCreateBy(warehousingApplication.getCreateBy());
            warehousingProduct.setWarehousingOrder(order);
            warehousingProductMapper.addWarehousingProduct(warehousingProduct);
            order++;
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteWarehousingApplication(WarehousingApplication warehousingApplication) {
        warehousingApplicationMapper.deleteWarehousingApplication(warehousingApplication);
        WarehousingProduct warehousingProduct =new WarehousingProduct();
        warehousingProduct.setApplyId(warehousingApplication.getApplyId());
        warehousingProduct.setModifyBy(warehousingApplication.getModifyBy());
        warehousingProductMapper.deleteWarehousingProduct(warehousingProduct);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteWarehousingApplications(BusinessOperationDeleteDto deleteDto) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",LoginDetailUtils.getCurrentEmployeeId().longValue());
        if(deleteDto.getMainIds()!=null&&deleteDto.getMainIds().length>0){
            map.put("applyIds",deleteDto.getMainIds());
            warehousingApplicationMapper.deleteWarehousingApplications(map);
        }
        if(deleteDto.getSonIds()!=null&&deleteDto.getSonIds().length>0){
            map.put("ids",deleteDto.getSonIds());
            warehousingProductMapper.deleteWarehousingProductList(map);
            for(Long id:deleteDto.getSonIds()){
                WarehousingProduct warehousingProduct=warehousingProductMapper.getWarehousingProductByIdAll(id);
                if(warehousingProduct!=null){
                    //查询删除子项的主表id下总入库数量和总数量的差来判断入库的状态
                    Integer differQuantity=warehousingApplicationMapper.getDifferQuantity(warehousingProduct.getApplyId());
                    if(differQuantity!=null) {
                        WarehousingApplication warehousingApplicationMy = new WarehousingApplication();
                        warehousingApplicationMy.setApplyId(warehousingProduct.getApplyId());
                        warehousingApplicationMy.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
                        if (differQuantity.intValue() == 0) {
                            warehousingApplicationMy.setOrderStatus(5);
                        } else {
                            warehousingApplicationMy.setOrderStatus(4);
                        }
                        warehousingApplicationMapper.updateWarehousingApplication(warehousingApplicationMy);
                    }
                }
            }
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateWarehousingApplication(WarehousingApplicationUpdateDto warehousingApplicationUpdateDto) {
        WarehousingApplication warehousingApplication = new WarehousingApplication();
        BeanUtils.copyProperties(warehousingApplicationUpdateDto, warehousingApplication);
        warehousingApplication.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        Supplier supplier=supplierMapper.querySupplierById(warehousingApplication.getSupplierId());
        if(supplier!=null){
            warehousingApplication.setSupplierCode(supplier.getSupplierCode());
            warehousingApplication.setSupplierName(supplier.getSupplierName());
        }
        warehousingApplicationMapper.updateWarehousingApplication(warehousingApplication);
        int order=1;
        for (WarehousingProductDto warehousingProductDto : warehousingApplicationUpdateDto.getWarehousingProductList()) {
            WarehousingProduct warehousingProduct=new WarehousingProduct();
            BeanUtils.copyProperties(warehousingProductDto, warehousingProduct);
            warehousingProduct.setApplyId(warehousingApplication.getApplyId());
            warehousingProduct.setWarehousingOrder(order);
            //没有子项主键id的做新增，有的则修改
            if(warehousingProduct.getId()==null||warehousingProduct.getId().longValue()==0L){
                warehousingProduct.setCreateBy(warehousingApplication.getModifyBy());
                warehousingProductMapper.addWarehousingProduct(warehousingProduct);
            }else{
                //WarehousingProduct warehousingProductMy=warehousingProductMapper.getWarehousingProductById(warehousingProduct.getId());
                //warehousingProduct.setResidueQuantity(warehousingProduct.getQuantity()-warehousingProductMy.getSelectSum()-warehousingProductMy.getSalesReturnQuantity());
                List<WarehousingReturnProduct> list=warehousingReturnProductMapper.getProductByInProductId(warehousingProduct.getId());
                Integer quantity=0;
                Double volume=0.0;
                Double weight=0.0;
                Integer tray=0;
                for (WarehousingReturnProduct warehousingReturnProduct : list) {
                    quantity=quantity+warehousingReturnProduct.getQuantity();
                    volume=volume+warehousingReturnProduct.getVolume();
                    weight=weight+warehousingReturnProduct.getWeight();
                    tray=tray+warehousingReturnProduct.getTray();
                }
                warehousingProduct.setResidueQuantity(warehousingProduct.getQuantity()-quantity);
                warehousingProduct.setResidueVolume(warehousingProduct.getInVolume()-volume);
                warehousingProduct.setResidueWeight(warehousingProduct.getInWeight()-weight);
                warehousingProduct.setResidueTray(warehousingProduct.getTray()-tray);
                warehousingProduct.setModifyBy(warehousingApplication.getModifyBy());
                warehousingProductMapper.updateWarehousingProduct(warehousingProduct);
            }
            order++;
        }
        return 1;
    }

    @Override
    public Page<WarehousingApplication> queryWarehousingApplication(Page<WarehousingApplication> page, WarehousingApplicationQueryDto warehousingApplication) {
        List<WarehousingApplication> warehousingApplications = warehousingApplicationMapper.queryWarehousingApplication(page,warehousingApplication);
        return page.setRecords(warehousingApplications);
    }

    @Override
    public WarehousingApplication queryWarehousingApplicationById(Long applyId) {
        return warehousingApplicationMapper.queryWarehousingApplicationById(applyId);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateAffirmOrCancel(WarehousingApplication warehousingApplication) {
        warehousingApplicationMapper.updateWarehousingApplication(warehousingApplication);
        WarehousingProduct warehousingProduct = new WarehousingProduct();
        warehousingProduct.setApplyId(warehousingApplication.getApplyId());
        warehousingProduct.setModifyBy(warehousingApplication.getModifyBy());
        warehousingProduct.setOrderStatus(warehousingApplication.getOrderStatus());
        warehousingProductMapper.updateConfirmToCancel(warehousingProduct);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateOrderWarehousing(List<BaseInStoreDetailAddDto> detailList, String requestCode) {
        Long employeeId=LoginDetailUtils.getCurrentEmployeeId().longValue();
        for(BaseInStoreDetailAddDto detail:detailList){
            WarehousingProduct warehousingProduct=warehousingProductMapper.getWarehousingProductById(detail.getApplyProductId());
            if(warehousingProduct==null){
                continue;
            }
            //入库操作后修改入库产品信息(数量，重量，体积等)
            WarehousingProduct warehousingProductMy=new WarehousingProduct();
            warehousingProductMy.setId(detail.getApplyProductId());
            warehousingProductMy.setSelectSum(warehousingProduct.getSelectSum()+Integer.parseInt(String.valueOf(detail.getAmount())));
            warehousingProductMy.setResidueQuantity(warehousingProduct.getResidueQuantity()-Integer.parseInt(String.valueOf(detail.getAmount())));
            warehousingProductMy.setResidueWeight(warehousingProduct.getResidueWeight()-detail.getWeight());
            warehousingProductMy.setResidueVolume(warehousingProduct.getResidueVolume()-detail.getVolume());
            warehousingProductMy.setResidueTray(warehousingProduct.getResidueTray()-Integer.parseInt(String.valueOf(detail.getTrays())));
            //如果总的入库数量与要入库的数量相同则全部入库状态
            if(warehousingProductMy.getSelectSum().intValue()==warehousingProduct.getQuantity().intValue()){
                warehousingProductMy.setOrderStatus(5);
            }else{
                warehousingProductMy.setOrderStatus(4);
            }
            warehousingProductMy.setModifyBy(employeeId);
            warehousingProductMapper.updateWarehousingProduct(warehousingProductMy);
        }
        //修改入库主表的状态信息
        WarehousingApplication warehousingApplication=warehousingApplicationMapper.queryWarehousingApplicationByNo(requestCode);
        if(warehousingApplication!=null){
            //查询主表下总入库数量和总数量的差来判断入库的状态
            Integer differQuantity=warehousingApplicationMapper.getDifferQuantity(warehousingApplication.getApplyId());
            if(differQuantity!=null) {
              WarehousingApplication warehousingApplicationMy = new WarehousingApplication();
              warehousingApplicationMy.setApplyId(warehousingApplication.getApplyId());
              warehousingApplicationMy.setModifyBy(employeeId);
              if (differQuantity.intValue() == 0) {
                warehousingApplicationMy.setOrderStatus(5);
              } else {
                warehousingApplicationMy.setOrderStatus(4);
              }
              warehousingApplicationMapper.updateWarehousingApplication(warehousingApplicationMy);
            }
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateOrderWarehousingReturn(List<WarehousingReturnProductDto> returnProductList, String requestCode) {
        Long employeeId=LoginDetailUtils.getCurrentEmployeeId().longValue();
        for(WarehousingReturnProductDto returnProduct:returnProductList){
            WarehousingProduct warehousingProduct=warehousingProductMapper.getWarehousingProductById(returnProduct.getInProductId());
            if(warehousingProduct==null){
                continue;
            }
            //退货操作后修改入库产品信息(数量，重量，体积等)
            WarehousingProduct warehousingProductMy=new WarehousingProduct();
            warehousingProductMy.setId(returnProduct.getInProductId());
            warehousingProductMy.setSalesReturnQuantity(warehousingProduct.getSalesReturnQuantity()+returnProduct.getQuantity());
            warehousingProductMy.setResidueQuantity(warehousingProduct.getResidueQuantity()-returnProduct.getQuantity());
            warehousingProductMy.setResidueWeight(warehousingProduct.getResidueWeight()-returnProduct.getWeight());
            warehousingProductMy.setResidueVolume(warehousingProduct.getResidueVolume()-returnProduct.getVolume());
            warehousingProductMy.setResidueTray(warehousingProduct.getResidueTray()-returnProduct.getTray());
            warehousingProductMy.setModifyBy(employeeId);
            warehousingProductMapper.updateWarehousingProduct(warehousingProductMy);
        }
        //修改入库申请主表的退货状态信息
        WarehousingApplication warehousingApplication=warehousingApplicationMapper.queryWarehousingApplicationByNo(requestCode);
        if(warehousingApplication!=null){
            WarehousingApplication warehousingApplicationMy = new WarehousingApplication();
            warehousingApplicationMy.setApplyId(warehousingApplication.getApplyId());
            warehousingApplicationMy.setModifyBy(employeeId);
            warehousingApplicationMy.setSalesReturnStatus(1);
            warehousingApplicationMapper.updateWarehousingApplication(warehousingApplicationMy);
        }
        return 1;
    }

    @Override
    public Long getMaxApplyId() {
        return warehousingApplicationMapper.getMaxApplyId();
    }

    @Override
    public Long getSumReturnQuantity(Long applyId) {
        return warehousingApplicationMapper.getSumReturnQuantity(applyId);
    }

    @Override
    public List<WarehousingApplicationExport> queryWarehousingApplicationExport(WarehousingApplicationQueryDto warehousingApplicationQueryDto) {
        return warehousingApplicationMapper.queryWarehousingApplicationExport(warehousingApplicationQueryDto);
    }
}
